package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entity.CommonResult;
import com.atguigu.springcloud.entity.Payment;
import com.atguigu.springcloud.service.LoadBalanced;
import com.netflix.ribbon.proxy.annotation.Http;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

/**
 * @author 孙志鹏
 * @createDate 2022/3/30 15:13
 */
@Slf4j
@RestController
@RequestMapping("/consumer")
public class OrderController {

    // public static final String PAYMENTSRV_URL = "http://localhost:8001/payment";

    // RestTemplate负载均衡机制
    public static final String PAYMENTSRV_URL = "http://CLOUD-PAYMENT-SERVICE/payment";

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private LoadBalanced loadBalanced;
    // @GetMapping("/create")
    // public CommonResult create(Payment payment) {
    //     return restTemplate.postForObject(PAYMENTSRV_URL,payment,CommonResult.class);
    // }

    @GetMapping("/payment/zipkin")
    public String paymentZipkin()
    {
        String result = restTemplate.getForObject("http://localhost:8001"+"/payment/zipkin/", String.class);
        return result;
    }

    @GetMapping("/create")
    public ResponseEntity<CommonResult> create(Payment payment) {
        return restTemplate.getForEntity(PAYMENTSRV_URL, CommonResult.class, payment);
    }

    @GetMapping("/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable Long id) {
        return restTemplate.getForObject(PAYMENTSRV_URL + "/" + id,CommonResult.class,id);
    }

    @GetMapping(value = "/payment/lb")
    public String getPaymentLb() {
        // 1.获取所有的服务总数
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        if (ObjectUtils.isEmpty(instances) || instances.size() == 0) {
            return null;
        }

        // 2. 获取服务的总数
        ServiceInstance serviceInstance = loadBalanced.instances(instances);
        URI uri = serviceInstance.getUri();
        return restTemplate.getForObject(uri + "/payment/lb", String.class);
    }

    // @GetMapping("/payment/getForEntity/{id}")
    // public CommonResult<Payment> getPayment2(@PathVariable Long id) {
    //     ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENTSRV_URL + "/" + id, CommonResult.class);
    //     if (entity.getStatusCode().is2xxSuccessful()) {
    //         return entity.getBody();
    //     } else {
    //         return new CommonResult<>(444,"操作失败");
    //     }
    // }
}
